
define(["jquery","zTree","jq_request","jqform","layuiall",'helper'], function ($,zTree,jq_request,jqform,layui,helper) {
    var form = layui.form;
    var zTreeObj,idList,nodeList;
    var setting = {
        async:{
            enable: false
            ,dataType:'json'
            ,type:'post'
            ,url:"/admin/system.menu/index"
            ,dataFilter:function (treeId, parentNode, d) {
                nodeList = d.data;
                return  d.data
            }
        },
        view: {
            showIcon: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey:"id",
                pIdKey:"parent_id",
            },
            key:{
                name:"title"
            }
        },
        check: {
            enable: false,
            chkStyle: "radio",
            radioType: "all"
        },
        callback: {
            onClick: function (e, treeId, treeNode, clickFlag) {
                idList = treeNode.id;
                zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                $(".app-right").show().find(".loadHtml").attr("src","/admin/system.menu/savemenu?id="+treeNode.id)
            }
        },
    };
    var Controller = {
        index:function () {
            $.ajax({
                type:'post',
                dataType:'json',
                success:function (res) {
                    nodeList = res.data;
                    zTreeObj = $.fn.zTree.init($("#MenuTree"), setting,nodeList);
                }
            })
            $(".actionBtn").click(function () {
                var dataType = parseInt($(this).attr("data-type"));
                var _this = $(this);
                switch (dataType){
                    case 1:
                        $(".app-right").show().find(".loadHtml").attr("src",_this.attr("data-url"));
                        break;
                    case 2:
                        if(!idList) return layer.msg("请选择所需要删除的数据");
                        layer.confirm('确定要删除数据吗', {icon: 3, title:'提示'}, function(index) {
                            $.ajax({
                                url:_this.attr("data-url")
                                ,dataType:'json'
                                ,data:{
                                    idList:idList
                                }
                                ,success:function (res) {
                                    layer.msg(res.msg);
                                    if(res.code===1){
                                        $(".app-right").hide();
                                        var index = array_search(nodeList,"id",idList)
                                        nodeList.splice(index,1);
                                        $.fn.zTree.init($("#MenuTree"),setting,nodeList);
                                    }
                                }
                            });
                        })
                        break;
                }
            })
            $(".ref-node").click(function () {
                layer.open({
                    skin:'refNode_iframe',
                    title:'菜单刷新',
                    type:2,
                    content:$(this).attr("data-url"),
                    area:["650px","450px"],
                    offset: '100px',
                    shadeClose:true
                })
            })
            jqform.delete()
            //数据全部选中
            form.on('checkbox(select-all)', function(data){

                if(data.elem.checked==true){
                    $("input[name='idList[]']").prop('checked',true)
                }else {
                    $("input[name='idList[]']").prop('checked',false)
                }
                form.render('checkbox');
            });
        },
        add: function () {
            level();
            logic()
            jqform.init(function (d) {
                getParentMenu(d)
            })
        },
        savemenu:function () {
            level();
            logic();
            jqform.init(function (d) {
                getParentMenu(d)
            })
            $(".deleteBtn").click(function () {
                layer.confirm('确定要删除数据吗', {icon: 3, title:'提示'}, function(index) {
                    $.ajax({
                        type:'post',
                        dataType:'json',
                        data:{
                            idList:$('input[name="id"]').val()
                        },
                        url:"/admin/system.menu/delmenu",
                        success:function(res){
                            $(".app-right",window.parent.document).hide();
                            getParentMenu(res)
                        }
                    })
                })
            })
        },
        refmenu:function () {
            $(".back").click(function () {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            })
        },
        importmenu:function () {
            $(".back").click(function () {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            })
        }
    };
    function getParentMenu(res) {
        if (window.frames.length != parent.frames.length ) {
            setting.callback.onClick = function (e, treeId, treeNode, clickFlag) {
                $(".app-right",window.parent.document).show().find(".loadHtml").attr("src","/admin/system.menu/savemenu?id="+treeNode.id)
            }
            $.ajax({
                type:'post',
                dataType:'json'
                ,url:"/admin/system.menu/index"
                ,success:function (res) {
                    nodeList = res.data;
                    zTreeObj = $.fn.zTree.init($("#MenuTree",window.parent.document), setting,nodeList);
                }
            })
        }
    }
    function array_search(arr,key,value) {
        for (var i=0;i<arr.length;i++){
            var a = arr[i][key];
            if(value==a){
                return i
            }
        }
    }
    //层级选择
    function level() {
        $(".menuLevel").click(function () {
            var TreeHtml = '<ul id="MenuTree" class="ztree"></ul>';
            var menuTreeAler = layer.open({
                type: 1,
                title: "层级选择",
                content: TreeHtml,
                area:["300px","350px"],
                shadeClose:true,
                success: function () {
                    jq_request.post($(this).attr("data-url"), {
                        data: {
                            menuType: 'tree'
                        },
                        success: function (res) {
                            var zTreeObj;
                            var setting = {
                                view: {
                                    showIcon: false
                                },
                                data: {
                                    simpleData: {
                                        enable: true,
                                        idKey:"id",
                                        pIdKey:"parent_id",
                                    },
                                    key:{
                                        name:"text"
                                    }
                                },
                                check: {
                                    enable: true,
                                    chkStyle: "radio",
                                    radioType: "all"
                                },
                                callback: {
                                    onClick: function (e, treeId, treeNode, clickFlag) {
                                        zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                                        nodes = zTreeObj.getCheckedNodes(true),
                                            v = "";
                                        for (var i = 0; i < nodes.length; i++) {
                                            v += nodes[i].id
                                            $("input[name='parent_id']").val(v).siblings("input").val( nodes[i].text)
                                        }
                                        layer.close(menuTreeAler)
                                    },
                                    onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                                        $("input[name='parent_id']").val(treeNode.id).siblings("input").val(treeNode.text)
                                        layer.close(menuTreeAler)
                                    },
                                }
                            };
                            zTreeObj = $.fn.zTree.init($("#MenuTree"), setting, res);
                            zTreeObj.expandAll(false);
                        }
                    })
                }
            })
        })
    }
    function logic() {
        $("#test1").click(function () {
            uploadFiles({
                url: '/admin/webupload/webuploader/multi/1/filetype/image/app/admin/tab/local',
                file:function (res) {
                    var file = JSON.parse(res);
                    console.log(file)
                    $("#app-photo").find("input[name='icon']").val(file[0].filepath);
                    $("#app-photo").find("img").attr("src",file[0].preview_url);
                    localStorage.removeItem("upload");
                }
            });
        })
    }
    return Controller;
});